package com.tencreat.common.config;

import com.tencreat.common.enums.QueueEnum;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息队列配置
 * Created by macro on 2018/9/14.
 */
@Configuration
public class RabbitMqConfig {

    @Bean
    DirectExchange dictDirect() {
        return ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_DICT.getExchange())
                .durable(true)
                .build();
    }


    @Bean
    public Queue dictQueue() {
        return new Queue(QueueEnum.QUEUE_DICT.getName());
    }

    @Bean
    Binding dictBinding(DirectExchange dictDirect,Queue dictQueue){
        return BindingBuilder
                .bind(dictQueue)
                .to(dictDirect)
                .with(QueueEnum.QUEUE_DICT.getRouteKey());
    }

    /**
     * 订单取消
     *
     * @return
     */
    @Bean
    public Exchange orderChangeExchange() {
        Map<String, Object> map = new HashMap<>(1);
        map.put("x-delayed-type", "direct");
        return new CustomExchange(QueueEnum.QUEUE_DELAYED_ORDER_CANCEL.getExchange(), "x-delayed-message",
                true, false, map);
    }

    @Bean
    public Queue orderChangeQueue() {
        return new Queue(QueueEnum.QUEUE_DELAYED_ORDER_CANCEL.getName(), true);
    }

    @Bean
    public Binding orderChangeBinding() {
        return BindingBuilder.bind(orderChangeQueue()).to(orderChangeExchange()).with(QueueEnum.QUEUE_DELAYED_ORDER_CANCEL.getRouteKey()).noargs();
    }

    /**
     * 将数据转换成json存储在消息队列中
     * @return
     */
//    @Bean
//    public MessageConverter messageConverter(){
//        return new Jackson2JsonMessageConverter();
//    }


    /**
     * 余额
     *
     * @return
     */
    @Bean
    public Exchange balanceChangeExchange() {
        Map<String, Object> map = new HashMap<>(1);
        map.put("x-delayed-type", "direct");
        return new CustomExchange(QueueEnum.BALANCE_CHANGE.getExchange(), "x-delayed-message",
                true, false, map);
    }

    @Bean
    public Queue balanceChangeQueue() {
        return new Queue(QueueEnum.BALANCE_CHANGE.getName(), true);
    }

    @Bean
    public Binding balanceChangeBinding() {
        return BindingBuilder.bind(balanceChangeQueue()).to(balanceChangeExchange()).with(QueueEnum.BALANCE_CHANGE.getRouteKey()).noargs();
    }
    /**
     * 业务action
     *
     * @return
     */
    @Bean
    public Exchange actionMessageExchange() {
        return new DirectExchange(QueueEnum.ACTION_MESSAGE.getExchange());
    }

    @Bean
    public Queue actionMessageQueue() {
        return new Queue(QueueEnum.ACTION_MESSAGE.getName(), true);
    }

    @Bean
    public Binding actionMessageBinding() {
        return BindingBuilder.bind(actionMessageQueue()).to(actionMessageExchange()).with(QueueEnum.ACTION_MESSAGE.getRouteKey()).noargs();
    }

    /**
     * 售后/退款退货
     * @return
     */
    @Bean
    public Exchange afterSaleBackExchangeConfirm() {
        Map<String, Object> map = new HashMap<>(1);
        map.put("x-delayed-type", "direct");
        return new CustomExchange(QueueEnum.QUEUE_DELAYED_AFTER_SALE_BACK.getExchange(), "x-delayed-message",
                true, false, map);
    }

    @Bean
    public Queue afterSaleBackQueueConfirm(){
        return new Queue(QueueEnum.QUEUE_DELAYED_AFTER_SALE_BACK.getName(),true);
    }

    @Bean
    public Binding afterSaleBackBindingConfirm(){
        return BindingBuilder.bind(afterSaleBackQueueConfirm()).to(afterSaleBackExchangeConfirm()).with(QueueEnum.QUEUE_DELAYED_AFTER_SALE_BACK.getRouteKey()).noargs();
    }

    /**
     * 售后/退款退货
     * @return
     */
    @Bean
    public Exchange afterSaleBackExchangeConfirmV2() {
        Map<String, Object> map = new HashMap<>(1);
        map.put("x-delayed-type", "direct");
        return new CustomExchange(QueueEnum.QUEUE_DELAYED_AFTER_SALE_CONFIRM.getExchange(), "x-delayed-message",
                true, false, map);
    }

    @Bean
    public Queue afterSaleBackQueueConfirmV2(){
        return new Queue(QueueEnum.QUEUE_DELAYED_AFTER_SALE_CONFIRM.getName(),true);
    }

    @Bean
    public Binding afterSaleBackBindingConfirmV2(){
        return BindingBuilder.bind(afterSaleBackQueueConfirmV2()).to(afterSaleBackExchangeConfirmV2()).with(QueueEnum.QUEUE_DELAYED_AFTER_SALE_CONFIRM.getRouteKey()).noargs();
    }

    /*积分*/
    @Bean
    public Exchange integralChangeExchange() {
        return new DirectExchange(QueueEnum.INTEGRAL_CHANGE.getExchange());
    }

    @Bean
    public Queue integralChangeQueue() {
        return new Queue(QueueEnum.INTEGRAL_CHANGE.getName(), true);
    }

    @Bean
    public Binding integralChangeBinding() {
        return BindingBuilder.bind(integralChangeQueue()).to(integralChangeExchange()).with(QueueEnum.INTEGRAL_CHANGE.getRouteKey()).noargs();
    }

    /**
     * 杉德代付查询
     *
     * @return
     */
    @Bean
    public Exchange sandExchange() {
        Map<String, Object> map = new HashMap<>(1);
        map.put("x-delayed-type", "direct");
        return new CustomExchange(QueueEnum.QUEUE_DELAYED_SAND_BEHALF.getExchange(), "x-delayed-message",
                true, false, map);
    }

    @Bean
    public Queue sandQueue() {
        return new Queue(QueueEnum.QUEUE_DELAYED_SAND_BEHALF.getName(), true);
    }

    @Bean
    public Binding sandBinding() {
        return BindingBuilder.bind(sandQueue()).to(sandExchange()).with(QueueEnum.QUEUE_DELAYED_SAND_BEHALF.getRouteKey()).noargs();
    }
}
